package com.example.xiaosuan.startproject_304fjl.lie;

import com.example.xiaosuan.startproject_304fjl.jiantingqi.fangwenwangluo;
import com.example.xiaosuan.startproject_304fjl.jiantingqi.weather_model;
import com.example.xiaosuan.startproject_304fjl.jiantingqi.weathersurver;
import com.example.xiaosuan.startproject_304fjl.jiantingqi.weathersurver2;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xiaosuan on 2017/3/21.
 */

public class model2 implements weather_model {
    private weathersurver2 service;
    private Retrofit retrofit;
    private static final String BASE_URL = "http://weather.51wnl.com/weatherinfo/";

    public model2() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getWeather(String cityNumber, final fangwenwangluo listener) {
        service = retrofit.create(weathersurver2.class);
        Call<weatherResult> call = service.getResult(cityNumber,1);
        call.enqueue(new Callback<weatherResult>() {
            @Override
            public void onResponse(Call<weatherResult> call, Response<weatherResult> response) {
                if (response.isSuccessful() && response.body() != null) {

                    listener.onResponse(response.body().getWeatherinfo());
                } else {
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<weatherResult> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

}
